<?php 
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-06-17 19:43:04 +0200 (Tue, 17 Jun 2008) $
 * $Rev: 1179 $
 * $Author: styck $
 * $Id: ajax_showpreview.php 1179 2008-06-17 17:43:04Z styck $
 */
define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);
$__auth = 'silent';

$pathajsp = realpath(dirname(__FILE__));

require_once "$pathajsp/../functions/html_includes.php";
require_once "$pathajsp/../functions/parse_nfo.php";

$STATUS = '';

// Display progress bar, if 100% complete then redirect to the dl link:

$dlid = isset($_GET['dlid']) ? $_GET['dlid'] : 0;
$binary_id = isset($_GET['binary_id']) ? $_GET['binary_id'] : 0;
$group_id = isset($_GET['group_id']) ? $_GET['group_id'] : 0;
$db->escape($dlid);
$sql = "SELECT * FROM downloadinfo WHERE \"ID\" = '$dlid'";
$res = $db->execute_query($sql);
if ($res === FALSE) 
	fatal_error($LN['error_noqueue'] );
$dl_info = $res[0];
$status = $dl_info['status'];

$sql = "SELECT * FROM queueinfo WHERE \"description\" = '" . get_command(COMMAND_DOWNLOAD_ACTION) . " $dlid' OR \"description\" = '"  .get_command(COMMAND_DOWNLOAD) . " $dlid'";
$res = $db->execute_query($sql);
if ($res === FALSE) 
	fatal_error($LN['error_noqueue'] );

$progress = $res[0]['progress'];

$isnzb = 0;

$rprefs = load_config($db);

function do_magic_nfo_extsetinfo(DatabaseConnection $db, $path, $file, $binary_id, $group_id, $userID)
{
	//	here we try to guess ext set info from the nfo file
	//
	assert (is_numeric($group_id) && $binary_id != '' && is_numeric($userID));
	$db->escape($binary_id, TRUE);
	$db->escape($group_id, FALSE);
	$sql = "SELECT \"setID\" FROM binaries_$group_id WHERE \"binaryID\" = $binary_id";
	$rv = $db->execute_query($sql);
	if($rv === FALSE) 
		return;

	$setID = $rv[0]['setID'] ;
	$origine_type = USERSETTYPE_GROUP;
	list($originalsetname, $groupname) = get_set_info($db, $setID, $origine_type);

	list($key,$val, $match) = guess_set_type($db, $setID, $groupname, $originalsetname);
	if ($val == 0) return;
	$save = parse_nfo_file($path . $file, $key);

	guess_more_data($val, $key, '', $groupname, $match, $save); 
	$save = array_map('utf8_encode', $save);
	store_ext_setdata($db, $setID, $save, $origine_type, ESI_NOT_COMMITTED);
	save_extsetinfo($db, $setID, $userID, $save, $origine_type, FALSE);
}

$do_reload = 1;

if ($status > DOWNLOAD_FINISHED && $status != DOWNLOAD_COMPLETE) { 
	$finished = -1;
	$progress = 0;
	$path = '';
	$files = array();
} else if ($status != DOWNLOAD_FINISHED) {
	$finished = 0;
	$progress = floor($progress);
	$path = '';
	$files = array();
} else {
	$do_reload = 0;
	$finished = 1;
	$progress = 100;
	$path = $dl_info['destination'];

	// First, do a manual check if we're not pointing to /tmp (this should always be to /preview)
	// It would be better if QUEUE_FINISHED was only set after it's moved to /preview but this is a workaround.

	$dlpath = get_dlpath($db);
	$previewpath = $dlpath . PREVIEW_PATH;
	if (substr($path, 0, strlen($previewpath)) == $previewpath) {
		// Everything cool, put all preview files into $files:
		$files = array();

		if ($handle = @opendir($path)) {
			while (FALSE !== ($file = readdir($handle))) {
				if (!in_array($file, array('.','..', URDD_DOWNLOAD_LOCKFILE))) {
					$files[] = $file;
					if (strtolower(substr($file, -4)) == '.nzb') 
						$isnzb ++;
					elseif (strtolower(substr($file, -4)) == '.nfo' && $rprefs['parse_nfo'] != 0) {
						if ($binary_id != '' && $group_id != 0)
							do_magic_nfo_extsetinfo($db, $path, $file, $binary_id, $group_id, $userID);
					}
				}
			}
			closedir($handle);
		}
	} else {
		// Downloaded file has not yet been moved from /tmp to /preview:
		$finished = 0;
		$progress = 99;
		$files = array();
		write_log('Reached preview page while download was not yet moved to /preview', LOG_NOTICE);
	}
}


$filename = isset($files[0]) ? $files[0] : '';

$smarty->assign('do_reload',	$do_reload);
$smarty->assign('finished',	$finished);
$smarty->assign('path',		$path);
$smarty->assign('isnzb', 	$isnzb);
$smarty->assign('file',		$filename);
$smarty->assign('file_utf8',	utf8_encode($filename));
$smarty->assign('progress',	$progress);
$smarty->assign('nroffiles',	count($files));

$smarty->display('ajax_preview.tpl');


?>
